package com.thiny.android.game.thunderbolt.ui;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.AlertDialog.Builder;
import android.app.Dialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ListView;

import com.thiny.android.game.thunderbolt.R;
import com.thiny.android.game.thunderbolt.adapter.ScoreAdapter;
import com.thiny.android.game.thunderbolt.util.Utils;

/*
 * 主菜单页面
 */
public class MainActivity extends Activity implements OnClickListener {

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);

		// 绑定监听器
		findViewById(R.id.start_game).setOnClickListener(this);
		findViewById(R.id.record).setOnClickListener(this);
		findViewById(R.id.sound).setOnClickListener(this);
		findViewById(R.id.help).setOnClickListener(this);
		findViewById(R.id.story).setOnClickListener(this);

	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.start_game:
			startActivity(new Intent(this, GameActivity.class));
			break;

		case R.id.record:
			showRecordDialog();
			break;

		case R.id.sound:
			showSoundDialog();
			break;

		case R.id.help:
			showHelpDialog();
			break;

		case R.id.story:
			showStoryDialog();
			break;

		}

	}

	// 记录对话框
	private void showRecordDialog() {

		View view = LayoutInflater.from(this).inflate(
				R.layout.dialog_list_view, null);
		ListView listView = (ListView) view.findViewById(R.id.dialog_listview);
		listView.setAdapter(new ScoreAdapter(this, Utils.getList()));

		if (Utils.getList() == null || Utils.getList().isEmpty()) {
			view.findViewById(R.id.list_layout).setVisibility(View.GONE);
			view.findViewById(R.id.no_record).setVisibility(View.VISIBLE);
		} else {
			view.findViewById(R.id.list_layout).setVisibility(View.VISIBLE);
			view.findViewById(R.id.no_record).setVisibility(View.GONE);
		}
		AlertDialog.Builder builder = new Builder(MainActivity.this);
		builder.setTitle(getString(R.string.game_record));
		builder.setView(view);
		builder.setPositiveButton(getString(R.string.ok),
				new android.content.DialogInterface.OnClickListener() {
					@Override
					public void onClick(DialogInterface dialog, int which) {
						dialog.dismiss();
					}
				});
		builder.create().show();
	}

	// 声音开关对话框
	private void showSoundDialog() {

		String[] items = new String[] { getString(R.string.on),
				getString(R.string.off) };

		Dialog dialog = null;
		AlertDialog.Builder builder = new Builder(MainActivity.this);
		builder.setTitle(getString(R.string.sound_choose));

		builder.setSingleChoiceItems(items,
				Utils.isMusicOn(MainActivity.this) ? 0 : 1,
				new android.content.DialogInterface.OnClickListener() {
					@Override
					public void onClick(DialogInterface dialog, int which) {
						Utils.saveMusicStatus(MainActivity.this,
								which == 0 ? true : false);
						dialog.dismiss();
					}
				});
		dialog = builder.create();
		dialog.show();
	}

	// 帮助对话框
	private void showHelpDialog() {
		View view = LayoutInflater.from(this).inflate(
				R.layout.help_layout, null);
		AlertDialog.Builder builder = new Builder(MainActivity.this);
		builder.setTitle(getString(R.string.game_help));
		builder.setView(view);
		builder.setPositiveButton(getString(R.string.ok),
				new android.content.DialogInterface.OnClickListener() {
					@Override
					public void onClick(DialogInterface dialog, int which) {
						dialog.dismiss();
					}
				});
		builder.create().show();
	}
	
	// 故事背景对话框
	private void showStoryDialog() {
		AlertDialog.Builder builder = new Builder(MainActivity.this);
		builder.setTitle(getString(R.string.story_title));
		builder.setMessage(getString(R.string.story_detail));
		builder.setPositiveButton(getString(R.string.ok),
				new android.content.DialogInterface.OnClickListener() {
					@Override
					public void onClick(DialogInterface dialog, int which) {
						dialog.dismiss();
					}
				});
		builder.create().show();
	}


	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		getMenuInflater().inflate(R.menu.main_menu, menu);
		return super.onCreateOptionsMenu(menu);
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		switch(item.getItemId()) {
		case R.id.about :
			startActivity(new Intent(this, AboutActivity.class));
			break;
		case R.id.exit :
			finish();
			break;
		 
		}
		return super.onOptionsItemSelected(item);
	}
	
	
}